/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.analysis;

import pl.tom.social.analyzer.graph.*;
import edu.uci.ics.jung.algorithms.importance.AbstractRanker;
import edu.uci.ics.jung.algorithms.importance.BaryCenter;
import edu.uci.ics.jung.algorithms.importance.BetweennessCentrality;
import edu.uci.ics.jung.algorithms.importance.DegreeDistributionRanker;
import edu.uci.ics.jung.algorithms.importance.HITS;
import edu.uci.ics.jung.algorithms.importance.NodeRanking;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.decorators.StringLabeller;
import java.util.Map;
import org.apache.commons.collections15.map.LinkedMap;
import pl.tom.social.analyzer.common.CentralityType;

/**
 *
 * @author Tom
 */
public class CentralityAnalysis {

	public static Map<String, Double> getScore(GraphNode graphHolder, CentralityType type) {
		Graph graph = graphHolder.getGraph();
		AbstractRanker ranker = null;

		switch (type) {
			case HITS:
				ranker = new HITS(graph);
				break;
			case BetweennessCentrality:
				ranker = new BetweennessCentrality(graph, true, false);
				break;
			case DegreeDistributionRanker:
				ranker = new DegreeDistributionRanker(graph);
				break;
			case BaryCenter:
				ranker = new BaryCenter(graph);
				break;
			default:
				throw new UnsupportedOperationException("Type not handled");
		}
		StringLabeller sl = StringLabeller.getLabeller(graph);
		return getRankings(ranker, sl);
	}

	private static Map<String, Double> getRankings(AbstractRanker ranker, StringLabeller sl) {
		Map<String, Double> result = new LinkedMap<String, Double>();
		ranker.evaluate();
		double max = 0;
		for (Object obj : ranker.getRankings()) {
			if (obj instanceof NodeRanking) {
				NodeRanking rank = (NodeRanking) obj;
				double score = rank.rankScore;
				result.put(sl.getLabel(rank.vertex), score);
				if(max < score) {
					max = score;
				}
			}
		}
		max /= 100;
		for(String s : result.keySet()) {
			result.put(s, result.get(s)/max);
		}
		return result;
	}
}
